// pages/mine/mine.js
const app = getApp()
const tools = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowUserName: false,
    userInfo: {
      avatar: '',
      user_name: ''
    },
    isCanShow: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let session3rd = wx.getStorageSync('session3rd');
    this.checkIsCanShow()
    if (session3rd) {
      this.setData({
        isShowUserName: true
      });
    }
    //获取用户信息
    let user_id = wx.getStorageSync('user_id');
    let user_name = wx.getStorageSync('user_name');
    let avatar = wx.getStorageSync('avatar');
    console.log('user_name',user_name)
    console.log('user_id', user_id)
    this.setData({
      userInfo: {
        avatar: avatar,
        user_name: user_name,
        user_id: user_id
      }
    });
    this.getUserProfile()
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getUserProfile(e) {
    let that = this;
    console.log('that:',that)
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log('res:',res)
        this.setData({
          userInfo: res.userInfo,
        })

        wx.login({
          success: (res) => {
            console.log('res',res)
            if (res.code) {
              wx.request({
                url: 'https://order.xiaxueruo.com/api/User/userInfo',
                data: {
                  code: res.code,
                  user_name: that.data.userInfo.nickName,
                  avatar: that.data.userInfo.avatarUrl,
                },
                method: 'GET',
                success: function (res) {
                  // 接口返回openid或者一个能标识身份的标识，比如user_id
                  console.log("成功：", res.data);
                  console.log("成功：", res.data.user_id);
                  console.log("成功：", res.data.user_name);
                  console.log("成功：", res.data.avatar);

                  // 把唯一标识缓存
                  wx.setStorageSync('session3rd', res.data.session3rd) 
                  wx.setStorageSync('user_id', res.data.user_id)
                  wx.setStorageSync('user_name', res.data.user_name)
                  wx.setStorageSync('avatar', res.data.avatar)
                                
                  that.setData({
                    userInfo: {
                      avatar: res.data.avatar,
                      user_name: res.data.user_name,
                      user_id: res.data.user_id
                    },
                    isShowUserName: true
                  });
                  wx.showToast({
                    title: '登录成功',
                    icon: 'fail',
                    duration: 2000//持续的时间
                  })
                }
              })
            }
          }
        })
        
      }

    })
  },
  
  reload:function(){
    if(!wx.getStorageSync('session3rd')){
      this.getUserProfile() //登录
    }else{
      let that=this;
      wx.showModal({
        title: '确认提示',
        content: '是否退出登录',
        success: function (res) { 
          if (res.confirm) {//这里是点击了确定以后
            that.setData({
              userInfo: {
                avatar: that.data.avatar,
                user_name: that.data.user_name,
              },
              isShowUserName:false,
            });
            wx.setStorageSync('user_id', '');
            wx.getStorageSync('user_name','');
            wx.getStorageSync('avatar','');
            wx.getStorageSync('session3rd','')
            wx.showToast({
              title: '退出登录成功',
              icon: 'success',
              duration: 2000//持续的时间
            })
            
          } else {//这里是点击了取消以后 
            wx.showToast({
              title: '取消成功',
              icon: 'fail',
              duration: 2000//持续的时间
            })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // console.log(tools.formatTime(new Date()))
    // console.log(getCurrentPages())
    wx.login({
      success: function (res) {
        // console.log(res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 事件处理函数
   */
  bindViewTap: function (e) {
    console.log(e)
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
 
  onShareTimeline: function (res) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  onShow(options) {
    this.getUserProfile()
    var user = wx.getStorageSync('user');
    if (user && user.user_name) {
      this.setData({
        isShowUserName: true,
        userInfo: user,
      })
    }
  },

  CopyLink(e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.link,
      success: res => {
        wx.showToast({
          title: '已复制',
          duration: 1000,
        })
      }
    })
  },

  order:function(){
    wx.navigateTo({
      url: '../order/order',
    })
    if (!wx.getStorageSync('user_id')) {
      this.getUserProfile()
    } 
  },

  /**
  * 获取接口数据，控制是否显示敏感信息
  */
 checkIsCanShow() {
  wx.request({
    url: 'https://order.xiaxueruo.com/api/order/isCanShow/',
    success: res => {
      this.setData({
        isCanShow: res.data
      })
    }
  })
}

})